#!/bin/bash
# Program:
#      Pull log file from the device
# History:
# 2013/08/27    Jay Fan     First release. Initial version
# 2013/08/28    Jay Fan     Second release. Pull MTBF log files

# Pull all log files with prefix alog under /data/
list=`adb shell ls /data/ | grep alog | grep -v last | tr -d '\r' `

if [ -n "${list}" ]; then
    for file in ${list}
    do
        echo "pull: /data/${file} -> ${file}"
        adb pull "/data/${file}"
    done
else
    echo "*** No alog file in this device. Skipped."
fi

# Pull last log
list=`adb shell ls /data/last_alog | tr -d '\r' `
exist=`echo ${list} | grep 'No such file or directory'`

if [ -z "${exist}" ]; then
    if [ -n "${list}" ]; then
        if [ ! -d last_alog ]; then
            mkdir last_alog
        fi
        adb pull /data/last_alog  last_alog
    fi
else 
    echo "*** No last_alog file in this device. Skipped."
fi

# Pull ANR trace log
list=`adb shell ls /data/anr | tr -d '\r' `
exist=`echo ${list} | grep 'No such file or directory'`

if [ -z "${exist}" ]; then
    if [ -n "${list}" ]; then
        if [ ! -d anr ]; then
            mkdir anr
        fi
        adb pull /data/anr  anr
    fi
else
    echo "*** No anr file in this device. Skipped."
fi

# Pull CrashLog generated by MTBF
list=`adb shell ls /ext_card/CrashLog | tr -d '\r' `
exist=`echo ${list} | grep 'No such file or directory'`

if [ -z "${exist}" ]; then
    if [ -n "${list}" ]; then
        if [ ! -d CrashLog ]; then
            mkdir CrashLog
        fi
        adb pull /ext_card/CrashLog CrashLog
    fi
else
    echo "*** No CrashLog in this device. Skipped."
fi

# Pull CrashDump generated by MTBF
list=`adb shell ls /ext_card/CrashDump | tr -d '\r' `
exist=`echo ${list} | grep 'No such file or directory'`

if [ -z "${exist}" ]; then
    if [ -n "${list}" ]; then
        if [ ! -d CrashDump ]; then
            mkdir CrashDump
        fi
        adb pull /ext_card/CrashDump CrashDump
    fi
else
    echo "*** No CrashDump in this device. Skipped."
fi

# Pull dropbox log
list=`adb shell ls /data/system/dropbox | tr -d '\r' `
exist=`echo ${list} | grep 'No such file or directory'`

if [ -z "${exist}" ]; then
    if [ -n "${list}" ]; then
        if [ ! -d dropbox ]; then
            mkdir dropbox
        fi
        adb pull /data/system/dropbox dropbox
    fi
else
    echo "*** No dropbox in this device. Skipped."
fi

# Pull hprof
list=`adb shell ls /data/misc/ | grep hprof | tr -d '\r' `

if [ -n "${list}" ]; then
    for file in ${list}
    do
        echo "pull: /data/misc/${file} -> ${file}"
        adb pull "/data/misc/${file}"
    done
else
    echo "*** No hprof file in this device. Skipped."
fi
